package com.mapaoer.user.controller;

import com.mapaoer.user.service.UserService;
import com.mapaoer.user.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 手机号+密码登录
     */
    @PostMapping("/login/phone")
    public ResponseEntity<ResponseResult<?>> loginByPhonePassword(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String password = params.get("password");
        String token = userService.loginByPhonePassword(phone, password);
        return ResponseEntity.ok(ResponseResult.success(token));
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/send-sms")
    public ResponseEntity<ResponseResult<?>> sendSmsCode(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        boolean success = userService.sendSmsCode(phone);
        return ResponseEntity.ok(ResponseResult.success(success));
    }

    /**
     * 手机号+短信验证码登录
     */
    @PostMapping("/login/sms")
    public ResponseEntity<ResponseResult<?>> loginBySmsCode(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String smsCode = params.get("smsCode");
        String token = userService.loginBySmsCode(phone, smsCode);
        return ResponseEntity.ok(ResponseResult.success(token));
    }

    /**
     * 微信登录
     */
    @PostMapping("/login/wechat")
    public ResponseEntity<ResponseResult<?>> loginByWechat(@RequestBody Map<String, String> params) {
        String code = params.get("code");
        String token = userService.loginByWechat(code);
        return ResponseEntity.ok(ResponseResult.success(token));
    }

    /**
     * 支付宝登录
     */
    @PostMapping("/login/alipay")
    public ResponseEntity<ResponseResult<?>> loginByAlipay(@RequestBody Map<String, String> params) {
        String authCode = params.get("authCode");
        String token = userService.loginByAlipay(authCode);
        return ResponseEntity.ok(ResponseResult.success(token));
    }
}
